package com.jiezhuo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.cache.RedisCache;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.Menu;
import com.jiezhuo.entity.User;
import com.jiezhuo.entity.YourengConfig;
import com.jiezhuo.service.LoginService;
import com.jiezhuo.service.UserService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@Api(tags = "用户登录")
public class LoginController {


    @Autowired
    private LoginService loginService;
    @Autowired
    private UserService userService;

    @ApiOperation("用户认证")
    @PostMapping("/user/login")
    public R login(
            @RequestBody User user
    )  {
        user.setUserid(user.getUserid());
        user.setUserName(user.getUserid());
        user.setPassword("Cmedu@2024");
    //        user.setUserid("00000001");
//        user.setUserName("00000001");
//        user.setPassword("Cmedu@2024");
        return loginService.login(user);
    }
    @ApiOperation("登出")
    @PostMapping("/user/loginOut")
    public R loginOut(HttpServletRequest request){
        return loginService.logout(request);
    }
    @ApiOperation("获得X-Access-Token")
    @PostMapping("/user/getAccessToken")
    public R getAccessToken() throws IOException {
        QueryWrapper<Object> objectQueryWrapper = new QueryWrapper<>();

        String accessToken=  loginService.getAccessToken();
        return R.ok().data("Access_Token",accessToken).code(2000);
    }

//    @GetMapping("getInf")
//    public  Object getInf(){
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//        Long userid = loginUser.getUser().getId();
//        User byId = userService.getById(userid);
//        byId.setPassword("");
//        List<Menu> menus = userService.selectRoleInfo(byId.getId());
//        Map<Long, List<Menu>> collect = menus.stream().collect(Collectors.groupingBy(
//                Menu::getParentId,
//                 Collectors.toList()
//        ));
//        List<Menu> startMenu = menus.stream().filter(o -> {
//            return o.getParentId() == 0;
//        }).collect(Collectors.toList());
////        for (Menu menu : startMenu) {
//////            menu.getId()
////        }
//        List<Menu> menu = getMenu(startMenu,collect);
//        byId.setMenus(menu);
//        return R.ok().code(2000).msg("查询成功").data("userInf",byId);
//    }
    public List<Menu> getMenu(List<Menu> list, Map<Long, List<Menu>> collect){
        for (Menu menu : list) {

            if(!StringUtils.isEmpty( collect.get(menu.getId()))){
                getMenu(collect.get(menu.getId()),collect);
                menu.setChildren(collect.get(menu.getId()));
            }

        }
        return list;
    }

}


